package com.cssw.fyzb.job.executor.service.pack;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.dao.acct.AcctDao;
import com.cssw.fyzb.core.dao.acct.RedPackDao;
import com.cssw.fyzb.core.dao.acct.RedPackDaoImpl;
import com.cssw.fyzb.core.eao.acct.AcctLogEao;
import com.cssw.fyzb.core.eao.acct.RedPackLogEao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.util.DateUtil;
import com.cssw.fyzb.job.executor.util.UuidUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: zzf
 * @time: 2021/1/11 11:22
 */
@Service
public class RedBackServiceImpl implements RedBackService {

    private static final Logger logger = LoggerFactory.getLogger(RedBackServiceImpl.class);

    @Autowired
    private RedPackDao redPackDao;

    @Autowired
    private RedPackLogEao redPackLogEao;

    @Autowired
    private AcctDao acctDao;

    @Autowired
    private AcctLogEao acctLogEao;

    /**
     * 私人红包退回
     *
     * @return void
     * @Param []
     **/
    @Override
    public void returnRedBack() {
        //获取还未领取的红包
        List<RedPackPerson> RedPack = redPackDao.listRedPackRerson();
        for (RedPackPerson r : RedPack) {
            try {
                Db.tx(() -> {
                    //计算时差
                    long cha = new Date().getTime() - r.getSendTime().getTime();
                    if (cha > 0) {
                        //转换为24小时制
                        double result = cha * 1.0 / (1000 * 60 * 60);
                        //判断是否超过24小时
                        if (result > 24) {
                            //超过2小时退回用户账户并记录日志
                            // 用户加果子
                            String transferType = "G";
                            acctDao.addBalance(r.getFromUserId(), transferType, r.getMoney(), r.getRedPackId().toString(),
                                    AcctTradeType.RETURN_PERSON_RED_PACK, BusiType.RECE_PERSON_RED_PACK);
                            // 日志记录
                            String packType = "1";
                            String changeType = "1";
                            String tradeName = "私人红包-退回";
                            //es日志
                            redPackLogEao.save(r.getFromUserId(), r.getFromUserId(), packType, changeType, r.getMoney(),
                                    r.getRedPackId().toString(), AcctTradeType.RETURN_PERSON_RED_PACK, tradeName);

                            //需要记录账单日志
                            Acct acct = acctDao.queryFAcct(r.getFromUserId());
                            acctLogEao.save(UuidUtil.get(), acct.getAcctId(), r.getFromUserId(), AcctType.ACCT_TYPE_G, r.getMoney(),
                                    Acct.FEE_IN_DE_ADD, r.getRedPackId().toString(), AcctTradeType.RETURN_PERSON_RED_PACK, AcctTradeType.RETURN_PERSON_RED_PACK_NAME,
                                    BusiType.SEND_PERSON_RED_PACK, new Date());

                            //Dao日志
                            redPackDao.saveRedPackLog(r.getFromUserId(), packType, changeType, r.getMoney(), r.getRedPackId().toString(),
                                    AcctTradeType.RETURN_PERSON_RED_PACK, tradeName);
                            //状态改为已过期
                            redPackDao.updateStatus(r.getRedPackId(), RedPackPerson.STATUS_TIMEOUT);
                        }
                    }
                    return true;
                });
            } catch (Exception e){
                logger.error("", e);
                logger.error(">>>>>>" + JSON.toJSONString(r));
            }
        }
    }

    @Override
    public void returnLiveRedBack() {

        //获取还未领取的主播红包
        List<CmfLiveRedPack> list = redPackDao.listCmfLiveRedPack();
        for (CmfLiveRedPack r : list) {
            try {
                //判断剩余金额
                if (r.getSurplus().compareTo(BigDecimal.ZERO) != -1) {
                    //计算时差
                    Db.tx(() -> {
                        long cha = new Date().getTime() - r.getEffTime().getTime();
                        if (cha > 0) {
                            //转换为24小时制
                            double result = cha * 1.0 / (1000 * 60 * 60);
                            //判断是否超过24小时
                            if (result > 24) {
                                //超过2小时退回用户账户并记录日志
                                // 用户加果子
                                String transferType = "G";
                                acctDao.addBalance(r.getFromUserId(), transferType, r.getSurplus(), r.getRedPackId().toString(),
                                        AcctTradeType.RETURN_LIVE_RED_PACK, BusiType.RECE_PERSON_RED_PACK);
                                // 日志记录
                                String packType = "1";
                                String changeType = "2";
                                String tradeName = "直播红包-退回";
                                //es日志
                                redPackLogEao.save(r.getFromUserId(), r.getFromUserId(), packType, changeType, r.getSurplus(),
                                        r.getRedPackId().toString(), AcctTradeType.RETURN_LIVE_RED_PACK, tradeName);

                                //需要记录账单日志
                                Acct acct = acctDao.queryFAcct(r.getFromUserId());
                                acctLogEao.save(UuidUtil.get(), acct.getAcctId(), r.getFromUserId(), AcctType.ACCT_TYPE_G, r.getSurplus(),
                                        Acct.FEE_IN_DE_ADD, r.getRedPackId().toString(), AcctTradeType.RETURN_LIVE_RED_PACK, AcctTradeType.RETURN_LIVE_RED_PACK_NAME,
                                        BusiType.SEND_LIVE_RED_PACK, new Date());

                                //Dao日志
                                redPackDao.saveRedPackLog(r.getFromUserId(), packType, changeType, r.getSurplus(), r.getRedPackId().toString(),
                                        AcctTradeType.RETURN_LIVE_RED_PACK, tradeName);

                                //状态改为已过期
                                redPackDao.updateLiveStatus(r.getRedPackId(), CmfLiveRedPack.STATUS_TIMEOUT);
                            }
                        }
                        return true;
                    });
                } else { //判断 剩余金额小于0
                    //状态改为已过期
                    redPackDao.updateLiveStatus(r.getRedPackId(), CmfLiveRedPack.STATUS_TIMEOUT);
                }
            } catch (Exception e){
                logger.error("", e);
                logger.error("", JSON.toJSONString(r));
            }
        }
    }
}
